home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The X-Philes (2nd Revision)
/
The X-Philes Number 1 (1995).iso
/
xphiles
/
hp48_2
/
chasm01.sha
/
chasm.c
next >
Wrap
C/C++ Source or Header
|
1995-03-23
|
2KB
|
152 lines
/*
* chasm.c - main module
*
* @(#)chasm.c 1.1 91/04/10
*
* Copyright (c) 1991 Steve Scherf
*
* Author: Steve Scherf
* Date: Wed Apr 10 22:53:11 PDT 1991
*
*/
#include <stdio.h>
#include <string.h>
#include "chasm.h"
char *whatstr = "@(#) chasm v0.1 Copyright (c) 1991 Steve Scherf";
int aflag; /* true if ascii output */
int oflag; /* true if redfined output filename */
char *infile;
char *outfile;
char *cout = "c.out";
char *cascii = "c.ascii";
char *ctmp = "c.tmp";
char *xstrrchr();
FILE *ifp;
FILE *ofp;
main(argc, argv)
int argc;
char **argv;
{
int i;
char *name;
name = *argv;
infile = argv[argc-1];
if(!--argc)
usage(name);
for(argv++; *argv && argc > 1; argv += 1 + i, argc -= 1 + i) {
if(**argv != '-')
usage(name);
i = 0;
while(*(++*argv))
switch(**argv) {
case 'a':
aflag++;
break;
case 'o':
if(!(outfile = *(argv+1)))
usage(name);
oflag++;
i++;
break;
default:
usage(name);
}
}
if(!oflag)
if(aflag)
outfile = cascii;
else
outfile = cout;
if((ifp = fopen(infile, "r")) == 0) {
fprintf(stderr, "%s: cannot open %s\n", name, infile);
exit(1);
}
if((ofp = fopen(ctmp, "w")) == 0) {
fprintf(stderr, "%s: cannot open %s\n", name, ctmp);
exit(1);
}
init_links();
yyparse();
chk_var();
if(chk_size() || (chk_val() + chk_lab()) || pars_err ||
!out_inst(ofp, aflag)) {
if(unlink(ctmp))
perror(ctmp);
printf("No output written to %s\n", outfile);
exit(1);
}
if(sunlink(outfile))
exit(1);
if(link(ctmp, outfile)) {
fprintf(stderr, "Cannot link %s to %s\n", ctmp, outfile);
sunlink(ctmp);
exit(1);
}
sunlink(ctmp);
}
/* unlink only if file exists */
sunlink(n)
char *n;
{
if(!access(n, 0) && unlink(n)) {
perror(n);
return -1;
}
return 0;
}
/* print usage message and exit */
usage(name)
char *name;
{
char *n;
if(n = xstrrchr(name, '/'))
n++;
else
n = name;
fprintf(stderr, "Usage: %s [-a] [-o output_file] input_file\n", n);
exit(1);
}
/* return a pointer to the char c in s, null if nonexistent */
char *
xstrrchr(s, c)
register char *s;
register char c;
{
register char *p = 0;
while(*s) {
if(*s == c)
p = s;
s++;
}
return p;
}